﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using TextEidtorI.Algorithm;
namespace TextEidtorI.Algorithm
{
    class Color
    {
        public void ChangeColor(System.Windows.Media.Color l, RichTextBox richBox, string keyword)
        {
            //设置文字指针为Document初始位置
            TextPointer position = richBox.Document.ContentStart;
            while (position != null)
            {
                //向前搜索,需要内容为Text
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    //拿出Run的Text
                    string text = position.GetTextInRun(LogicalDirection.Forward);
                    //可能包含多个keyword,做遍历查找
                    int index = 0;
                    index = text.IndexOf(keyword, 0);
                    if (index != -1)
                    {
                        TextPointer start = position.GetPositionAtOffset(index);
                        TextPointer end = start.GetPositionAtOffset(keyword.Length);
                        position = selecta(l, richBox, keyword.Length, start, end);
                    }
                }
                //文字指针向前偏移
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
        }

        public TextPointer selecta(System.Windows.Media.Color l, RichTextBox richTextBox1, int selectLength, TextPointer tpStart, TextPointer tpEnd)
        {
            TextRange range = richTextBox1.Selection;
            range.Select(tpStart, tpEnd);
            //高亮选择
            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(l));
            range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            return tpEnd.GetNextContextPosition(LogicalDirection.Forward);
        }

        public string[] GetBlueKeyWords() // List from endelsebegin
        {//union all  @ as  varchar [ ] .column_id sys.indexes  sys.index_columns
            string[] res = {"ADD"};

            return res;
        }

    }
}
